package com.utils.basehelper.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(smdate);
		long smTime = calendar.getTimeInMillis();
		calendar.setTime(bdate);
		long bdTime = calendar.getTimeInMillis();
		return Integer.parseInt(String.valueOf((bdTime - smTime) / (1000 * 3600 * 24)));
	}

	/**
	 * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
	 * 
	 * @param nowTime
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
			return true;
		}
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		return date.after(begin) && date.before(end);
	}

	/**
	 * 获取指定日期所在月份开始的时间 时间格式yyyyMMdd
	 *
	 * @param date
	 *            指定日期
	 * @return
	 */
	public static Date getMonthBegin(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		// 设置为1号,当前日期既为本月第一天
		c.set(Calendar.DAY_OF_MONTH, 1);
		// 将小时至0
		c.set(Calendar.HOUR_OF_DAY, 0);
		// 将分钟至0
		c.set(Calendar.MINUTE, 0);
		// 将秒至0
		c.set(Calendar.SECOND, 0);
		// 将毫秒至0
		c.set(Calendar.MILLISECOND, 0);
		// 获取本月第一天的时间
		// return
		// com.jiaoyinbrother.util.DateUtil.getFormatYMDHMS().format(c.getTime());
		return c.getTime();
	}

	/**
	 * 获取指定日期所在月份结束的时间 时间格式yyyyMMdd
	 *
	 * @param date
	 *            指定日期
	 * @return
	 */
	public static Date getMonthEnd(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		// 设置为当月最后一天
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		// 将小时至23
		c.set(Calendar.HOUR_OF_DAY, 23);
		// 将分钟至59
		c.set(Calendar.MINUTE, 59);
		// 将秒至59
		c.set(Calendar.SECOND, 59);
		// 将毫秒至999
		c.set(Calendar.MILLISECOND, 999);
		// 获取本月最后一天的时间
		// return
		// com.jiaoyinbrother.util.DateUtil.getFormatYMDHMS().format(c.getTime());
		return c.getTime();
	}

	/**
	 * 计算两个日期之间相差几个月
	 *
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static long getMonthBetween(Date smdate, Date bdate) throws ParseException {
		long monthday;
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = fmt.parse(fmt.format(smdate));
		Calendar starCal = Calendar.getInstance();
		starCal.setTime(startDate);

		int sYear = starCal.get(Calendar.YEAR);
		int sMonth = starCal.get(Calendar.MONTH);
		int sDay = starCal.get(Calendar.DAY_OF_MONTH);
		Date endDate = fmt.parse(fmt.format(bdate));
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);
		int eYear = endCal.get(Calendar.YEAR);
		int eMonth = endCal.get(Calendar.MONTH);
		int eDay = endCal.get(Calendar.DAY_OF_MONTH);

		monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));

		//这里计算零头的情况，根据实际确定是否要加1 还是要减1
		if (sDay < eDay) {
			monthday = monthday + 1;

		}
		return monthday;
	}

}
